const app = getApp();
Page({
  data: {
    server:app.globalData.server,
    api:app.globalData.api,
    productId:null,
    goodItem:{},
    showBottom: false,//显示购物box
    buyNum:1,
    isLogin:false,
    token:'',
    userId:null
  },
  onLoad(query) {
   
    if(query.index==undefined){
      my.reLaunch({ url: '../index/index' });
      return 
    }
    if(query.pid){//如果是别人分享的
      let shareInfo = {
        pid:query.pid,
        productId:query.index
      }
      my.setStorage({
        key: 'shareInfo',
        data: shareInfo
      });
    }
    this.setData({
      productId:query.index
    })
    let that = this;
    my.getStorage({
      key: 'userInfo',
      success: function(res) {
        that.setData({
          isLogin:res.data?true:false,
          token:res.data?res.data.token:'',
          userId:res.data?res.data.userInfo.id:null,
        })
        //根据index去调该商品的详情，赋值给goodItem
        that.getProductDetail()
      }
    });
    
  },
  //点击触发打开购物box
  async onButtomBtnTap(e) {
    try{
      //判断token是否过期
      let result = await this.getTokenStatus();
      if(!this.data.isLogin){
        my.reLaunch({ url: '../login/login' });
        return false
      }
      this.setData({
        showBottom: true,
      });
    }catch(e){
      console.log(e)
    }
  },
  //关闭购物box
  onPopupClose() {
    this.setData({
      showBottom: false
    });
  },
  //数量增减
  callBackFn(value) {
    this.setData({
      buyNum:value
    })
  },
  //点击购物box的确定
  async bindOrder(){
    try{
      let result = await this.getTokenStatus();
      if(!this.data.isLogin){//token过期
        this.setData({
          showBottom: false
        });
        my.reLaunch({ url: '../login/login' });
        return false
      }
      // console.log(this.data.buyNum)
      // //立即购买
      // console.log('立即购买')
      //带着商品数据
      let product = {
        id:this.data.goodItem.id,
        pic:this.data.goodItem.pic,
        disc:this.data.goodItem.disc,
        price:this.data.goodItem.price,
        num:this.data.buyNum
      }
      console.log(product)
      my.setStorage({
        key: 'orderInfo',
        data: product
      });
      //跳转到确认订单页
      my.navigateTo({ url: '../confirm-order/confirm-order' })
    }catch(e){
      console.log(e)
    } 
  },
  onShareAppMessage(){
    return {
      desc: '我刚刚跟你分享了一件好物，快来看看吧',
      path: 'pages/detail/detail?pid='+this.data.userId+'&index='+this.data.productId 
    }
  },
  bindShare(){
    if(!this.data.isLogin){
      my.reLaunch({ url: '../login/login' });
      return false
    }else{
      this.onShareAppMessage()
    }
  },
  getProductDetail(){
    let that = this;
    my.request({
      url:this.data.api+'/product/detail?productId='+this.data.productId,
      method: 'get',
      headers:{
        'content-type':'application/json',
        'token':this.data.token
      },
      dataType: 'json',
      success: function(res) {
        console.log(res)
        if(!res.data.result.level){
          my.removeStorage({
            key: 'userInfo'
          });
          that.setData({
            isLogin:false,
            token:'',
            userId:null
          })
        }
        if(res.data.errCode==0){
          that.setData({
            goodItem:res.data.result
          })
        }
      }
    });
  },
  getTokenStatus(){
    return new Promise((resolve, reject) => {
      let that = this;
      my.request({
        url:this.data.api+'/checkToken',
        method: 'get',
        headers:{
          'content-type':'application/json',
          'token':this.data.token
        },
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.errCode==9){//token过期
            my.removeStorage({
              key: 'userInfo'
            });
            that.setData({
              isLogin:false,
              token:'',
              userId:null,
            })
            my.showToast({
              type: 'exception',
              content: res.data.message,
              duration: 2000
            });
          }
          //操作
          if(!that.data.isLogin){
            my.reLaunch({ url: '../login/login' });
          }
          resolve()
        }
      });
    })
  }

});
